package com.nice.nice201110.service;

import com.nice.nice201110.entity.TBaseDisease;
import com.nice.nice201110.entity.TLog;
import com.nice.nice201110.mapper.TBaseDiseaseMapper;
import com.nice.nice201110.mapper.TLogMapper;
import com.nice.nice201110.utils.NiceTableEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Service
public class TbaseDiseaseService {

    @Autowired
    TBaseDiseaseMapper tBaseDiseaseMapper;

    @Autowired
    TLogMapper tLogMapper;

    @Autowired
    private PlatformTransactionManager transactionManager;

    private TLog tLog = new TLog();

    /**  查询数据 */
    public List<TBaseDisease> getlist(BaseVo baseVo){
        return tBaseDiseaseMapper.selectByPrimaryKeys(baseVo);
    }
    /** 查询单一数据 */
    public TBaseDisease getOne(UserVo userVo,Integer id){
        TBaseDisease tBaseDisease = tBaseDiseaseMapper.selectByPrimaryKey(id);
        if(tBaseDisease!=null){
            tLog.setDate(new Date());
            tLog.setType(3);  //操作类型（新增0，修改1，删除2,查询3）
            tLog.setDocid(tBaseDisease.getId());
            tLog.setUsername(userVo.getName());
            tLog.setUserid(Integer.valueOf(userVo.getId()));
            tLog.setTtable(NiceTableEnum.form_base_disease);
            tLog.setRemark("查看疾病信息:"+tBaseDisease.toString());
            tLogMapper.insertSelective(tLog);
        }
        return tBaseDisease;
    }
    /** 插入数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean insertTbaseDisease(UserVo userVo,TBaseDisease tBaseDisease){
        try {
            int num = tBaseDiseaseMapper.insertSelective(tBaseDisease);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(0);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tBaseDisease.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_base_disease);
                tLog.setRemark("新增疾病信息:"+tBaseDisease.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 更新数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean updateTbaseDisease(UserVo userVo,TBaseDisease tBaseDisease){
        try {
            int num = tBaseDiseaseMapper.updateByPrimaryKeySelective(tBaseDisease);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(1);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tBaseDisease.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_base_disease);
                tLog.setRemark("修改疾病信息:"+tBaseDisease.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 删除单一数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean deletetBaseDisease(UserVo userVo,Integer id){
        try {
            int num = tBaseDiseaseMapper.deleteSelective(id);
            if (num > 0) {
                tLog.setDate(new Date());
                tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(id); //  数据id
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_base_disease);
                tLog.setRemark("删除疾病信息:" + tBaseDiseaseMapper.selectByPrimaryKey(id).toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 删除多数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean deletetsBaseDisease(UserVo userVo,String ids){
        String StrArr [] = ids.split(",");
        try {
            for(String strID:StrArr) {
                if(strID!=null && strID.trim().length()>0) {
                    int id = Integer.valueOf(strID);
                    int num = tBaseDiseaseMapper.deleteSelective(id);
                    if (num > 0) {
                        tLog.setDate(new Date());
                        tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                        tLog.setDocid(id); //  数据id
                        tLog.setUsername(userVo.getName());
                        tLog.setUserid(Integer.valueOf(userVo.getId()));
                        tLog.setTtable(NiceTableEnum.form_base_disease);
                        tLog.setRemark("删除疾病信息:" + tBaseDiseaseMapper.selectByPrimaryKey(id).toString());
                        tLogMapper.insertSelective(tLog);
                    }else{
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }

}
